<template>
  <div id="app">
		<transition name="fade"
		            mode="out-in">
			<router-view></router-view>
		</transition>
  </div>
</template>

<script>
import Svc from './service/index.js'
export default {
  name: 'app',
  methods: {
		fnGetParam(list) {
			var gw = ''
			for(var i = 0; i < list.length; i++) {
				var item = list[i].split("=")
				if(item[0] == 'gw') {
					gw = item[1]
					break
				}
			}
			return gw
		},
	//显示message提示框
    openMessage : function(msg,type){
        this.$message({
          	showClose: true,
          	message: msg,
          	type: type
        });
    },
    //判断菜单是否展示
    fnShowMenu(data){
    	var routes = this.$router.options.routes
    	//当前用户为审核人
    	if(data.IsApprover) {
    		for(var i = 0; i < routes.length; i++) {
    			var item = routes[i]
    			if(item.name =="会议室" || item.name =="个人中心") {
    				item.hidden = false
					for(var j = 0; j < item.children.length; j++) {
    					item.children[j].hidden = false
					}
	        	}
			}
    	}
    	
    	//当前用户为管理员
    	if(data.IsAdmin) {
    		for(var i = 0; i < routes.length; i++) {
    			var item = routes[i]
    			if(item.path =="/home") {
    				item.hidden = false
					for(var j = 0; j < item.children.length; j++) {
						if(item.children[j].path !='/review') {
        					item.children[j].hidden = false
            			}
					}
				}
			}
    	}
    	
    	//当前用户非管理员、非审核人
    	if (!data.IsAdmin && !data.IsApprover) {
    		for(var i = 0; i < routes.length; i++) {
    			var item = routes[i]
    			if(item.name =="会议室" || item.name =="个人中心") {
    				item.hidden = false
					for(var j = 0; j < item.children.length; j++) {
						if (item.children[j].name !="审核") {
							item.children[j].hidden = false
						}
					}
    			}
				}
    	}
    }
	},
	mounted() {
		var that = this
		var param = window.location.search.substring(1)
		if(param) {//从其他网址跳转过来->已登录->跳转到Home页
			if (!sessionStorage.getItem('user')) {
					var gw = that.fnGetParam(param.split('&'))
					var empid = gw.split('&')[0]
			    Svc.fnGetData('/api/user/'+empid,{},function(res){
			    	if (res.body.ErrCode == 0){
		    			//保存用户信息
		    			sessionStorage.setItem('user', JSON.stringify(res.body.Data));
		    			//菜单设置
		    			that.fnShowMenu(res.body.Data)
		    			that.$router.push({ path: '/workflow' });
			    	} else {
			    		that.openMessage(res.body.ErrMsg,'error')
			    	}
			    },function(res){
			    	that.openMessage("网络错误，请重新！",'error')
			    })
			} else {
					//菜单设置
    			that.fnShowMenu(JSON.parse(sessionStorage.getItem('user')))
					that.$router.push({ path: '/workflow' });
			}
		} else {
			if (!sessionStorage.getItem('user')) {
					//未登录->跳转到登录页
					that.$router.push({ path: '/' });
			}
		}
	}
}
</script>

<style scoped>
	body {
		margin: 0px;
		padding: 0px;
		font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
		font-size: 14px;
		-webkit-font-smoothing: antialiased;
	}
	
	#app {
		position: absolute;
		top: 0px;
		bottom: 0px;
		left: 0;
		right: 0;
		width: 100%;
		height: 100%;
	}
</style>
